cmake_minimum_required(VERSION 3.8)
project(sensor_cpp_pkg)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(sensor_msgs REQUIRED)

# 生成消息接口
rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/SensorData.msg"
)

# 传感器HCD5014发布者节点
add_executable(sensor_hcd5014_publisher 
  src/sensor_hcd5014_publisher.cpp
  src/modbus_rtu.cpp
)
# 链接消息接口
target_include_directories(sensor_hcd5014_publisher PRIVATE
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_cpp"
)
target_link_libraries(sensor_hcd5014_publisher
  ${PROJECT_NAME}__rosidl_typesupport_cpp
)
# 链接类型支持库
rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(sensor_hcd5014_publisher ${typesupport_target})
ament_target_dependencies(sensor_hcd5014_publisher 
  rclcpp 
  std_msgs
)
install(TARGETS sensor_hcd5014_publisher DESTINATION lib/${PROJECT_NAME})

# 传感器HCD5014订阅者节点
add_executable(sensor_hcd5014_subscriber 
  src/sensor_hcd5014_subscriber.cpp
)
# 链接消息接口
target_include_directories(sensor_hcd5014_subscriber PRIVATE
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp"
  "${CMAKE_CURRENT_BINARY_DIR}/rosidl_typesupport_cpp"
)
target_link_libraries(sensor_hcd5014_subscriber
  ${PROJECT_NAME}__rosidl_typesupport_cpp  # 直接链接项目自身的类型支持库
)
# 链接类型支持库
rosidl_get_typesupport_target(typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(sensor_hcd5014_subscriber ${typesupport_target})

ament_target_dependencies(sensor_hcd5014_subscriber 
  rclcpp 
  std_msgs
)
install(TARGETS sensor_hcd5014_subscriber DESTINATION lib/${PROJECT_NAME})

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  set(ament_cmake_copyright_FOUND TRUE)
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()
